package smtp_package;

/**
 * @author Cristovao Rufino
 * Protocolo similar ao SMTP
 */

import general.CFGLodader;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class smtpCliente {
	
	private String mailFrom;
	private String rcptTo;
	private String remoteServer;
	
	private PrintWriter buffW;
	private BufferedReader buffR;
	
	/**
	 * @author Cristovao Rufino
	 * @param String mailFrom - Endereco de e-mail de origem
	 * @param String rcptTo - Endereco de e-mail de destino
	 * Inicia o objeto smtpCliente com os enderecos de origem e destino
	 */
	public smtpCliente(String mailFrom, String rcptTo){
		this.mailFrom = mailFrom;
		this.rcptTo = rcptTo;
		this.remoteServer = getRemoteHost(rcptTo);

	}
	
	/**
	 * @author Cristovao Rufino
	 * @param inetAdd - Endereco de e-mail completo
	 * @return String contendo o servidor remoto, ou nulo se for incorreto
	 */
	
	public String getRemoteHost(String inetAdd){
		if(!inetAdd.equals(""))
			return inetAdd.substring(inetAdd.indexOf('@') + 1);
		
		return null;
	}
	
	/**
	 * @author Cristovao Rufino
	 * @param String message - Mensagem a ser enviada para o servidor
	 * Envia um e-mail para o servidor especificado na inicializacao da
	 * classe.
	 */
	
	public void sendEmail(String message){
		
		Socket socket = null;
		InputStream in = null;
		OutputStream out = null;
		String extraData = null;
		
		try{
			CFGLodader.CFGLoader();
			socket = new Socket( this.remoteServer, CFGLodader.SMTP_DEFAULT_PORT );
			
			in = new DataInputStream(socket.getInputStream());
			out = new DataOutputStream(socket.getOutputStream());
			
			buffW = new PrintWriter(new OutputStreamWriter(out));
			buffR = new BufferedReader(new InputStreamReader(in));
			
			sendMessage("helo " + socket.getLocalSocketAddress().toString());

			extraData = buffR.readLine(); //Greetings do servidor
			System.out.println(extraData);
			sendMessage("mail from <" + this.mailFrom + ">");

			extraData = buffR.readLine();
			System.out.println(extraData);
			sendMessage("rcpt to <" + this.rcptTo + ">");
			
			extraData = buffR.readLine();
			System.out.println(extraData);
			sendMessage("data");
			
			extraData = buffR.readLine();
			System.out.println(extraData);
			sendMessage(message); //Obs: o cliente deve preparar a mensagem antes
								//de passa-la para entrega no servidor SMTP!
			sendMessage("."); //Finaliza a escrita da mensagem
			
			extraData = buffR.readLine();
			System.out.println(extraData);
			sendMessage("quit");
			
		}
		catch (UnknownHostException e){
			
			System.out.println("Host remoto " + this.remoteServer + " desconhecido!");
		}
		
		catch (Exception e) {
				System.out.println(e.getMessage());
		}	
		
		try {
			if(buffW != null) buffW.close();
			if(buffR != null) buffR.close();
			if(in != null) in.close(); 
			if(out != null) out.close();
			if(socket != null) socket.close();
		}
		catch(Exception e){ }
			
	}//sendEmail
	
	private void sendMessage(String message){
		this.buffW.write(message + "\n");
		this.buffW.flush();
	}
	
}
